﻿using System;
using System.Windows;
using SmartSpace.Silverlight;
using SmartSpace.Silverlight.Shell.ServiceRef;

namespace SmartSpace.Silverlight.Shell
{
    /// <summary>
    /// 登陆页面
    /// </summary>
    public partial class LoginView : SlView
    {
        public LoginView()
        {
            InitializeComponent();
        }

        private void OnButtonLoginClick(object sender, RoutedEventArgs e)
        {
            // 获取UI数据
            string identifier = m_textBoxIdentifier.Text.Trim();
            string password = m_textBoxPassword.Password.Trim();
            
            // 创建代理对象
            SlWebServiceSoapClient service = App.CreateSlWebServiceProxy();
            service.AuthenticateCompleted += new EventHandler<AuthenticateCompletedEventArgs>(OnAuthenticateCompleted);

            // 发送验证请求
            service.AuthenticateAsync(identifier, password);
        }

        void OnAuthenticateCompleted(object sender, AuthenticateCompletedEventArgs e)
        {
            if (e.Result.IsSuccess)
            {
                AppData.User = new SlUser();
                AppData.User.Account = e.Result.Data.Account;
                AppData.User.Name = e.Result.Data.Name;
                AppData.User.Email = string.Empty;
                AppData.User.Phone = string.Empty; ;

                AppData.Applications = null;
                AppData.CurrentApplication = null;
                //AppData.Rights = null;
                AppData.Navigations = null;
                //AppData.ModuleAssemblies.Clear();

                RootPage.Instance.SwitchView(new LoadingView());
            }
            else
            {
                MessageBox.Show(e.Result.Message);
            }
        }
    }
}
